package jsmbscanner;

/*
 * GNU GENERAL PUBLIC LICENSE
 * Version 3, 29 June 2007
 * 
 * Copyright (C) 2009, vlad
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


import jcifs.UniAddress;
import jcifs.netbios.NbtAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author vlad
 */
public class NetworkWorker implements Callable<String> {

    static boolean errorOutput = false;
    // Get from host his name of Workgroup

    private String getWorkgroup(String targetIP) {
        String res = "<*>";
        NbtAddress[] addrs = null;
        try {
            addrs = NbtAddress.getAllByAddress(targetIP);
            for (int jj = 0; jj < addrs.length; jj++) {
                boolean GroupAddr = false;
                GroupAddr = addrs[jj].isGroupAddress();
                if (GroupAddr) {
                    res = addrs[jj].toString();
                    res = res.substring(0, res.indexOf("<"));
                    break;
                }
            }
        } catch (UnknownHostException ex) {
            System.out.println("Exception(getWorkgroup) is..." + ex);
            if (errorOutput == true) {
                System.out.println("getWorkgroup!\n");
                Logger.getLogger(JSmbScannerView.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return res;
    }
    // Main thread process
    String name;
    int networkTimeout;
    String ipString;
    String result;

    NetworkWorker(String threadname,
            String full_ip,
            int timeOut) {
        name = threadname;
        networkTimeout = timeOut;
        ipString = full_ip;
        System.out.println(" * " + name + " has started  ");

    }

    public String call() {
        String dns_name = "-";     //FQDN
        String nbt_name = "-";
        String work_group = "-";
        String mac = "*";
        String answer = "";
        try {
            InetAddress Address = InetAddress.getByName(ipString);
            //Is host reachable?
            boolean status = Address.isReachable(networkTimeout);
            //boolean status = Address.isReachable(null, 5, networkTimeout);
            if (status) { // Host is accessed
                // Get DNS name
                dns_name = Address.getHostName();
                // Get NetBIOS data
                UniAddress ua = null;
                ua = UniAddress.getByName(ipString);
                NbtAddress nbt_addr = NbtAddress.getByName(ipString);
                // NBT name
                nbt_name = ua.getHostName();
                if (!nbt_name.equals(ipString)) {
                    //System.out.println("NBT name =\"" + nbt_name + "\"");
                    // WORKGROUP
                    work_group = getWorkgroup(ipString);
                    // MAC address
                    byte[] array1 = nbt_addr.getMacAddress();
                    mac = InetTools.MACtoString(array1);
                } else {
                    work_group = "-";
                    mac = "-";
                    nbt_name = "-";
                }

                // Trasnfer information
                answer = "true/" + nbt_name + "/" + work_group + "/" + dns_name + "/"
                        + mac + "/" + ipString;
            } else {
                // Host is unreacheble
                answer = "false";
            }

            System.out.println("Thread " + name + " got result for " + ipString + " :" + answer);
            result = answer;
        } catch (Exception ex) {
            System.out.println("I've caught exception. It is..." + ex);
            if (errorOutput) {
                Logger.getLogger(JSmbScannerView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return result;
    }
}
